import { fetch, post } from '../../../utils/axios'
const state = {
  selectAll: false,
  selected: [],
  publish: [],
  value: 0,
  interval: {},
  mutiPubDialog: false,
  mutiPubTitle: '此操作责任重大，请确认已经做好了背锅的准备再点击确认！'
}

const mutations = {
  toggleAll (state) {
    if (state.selectAll) {
      let result = []
      state.publish.every(function (pub) {
        if (pub.status === 0) {
          result.push(pub.id)
        }
        return pub.id
      })
      state.selected = result
    } else {
      state.selected = []
    }
  },
  loadPublishList (state) {
    fetch('/publisher/get_today_publish/', {
    }).then((response) => {
      state.publish = response.rows
      state.value = response.progress
    }).catch((error) => {
      alert(error.response)
    })
  },
  doMutiPublish (state) {
    if (state.selected.length > 0) {
      post('/publisher/do_muti_roll_publish/',
        state.selected
      ).then((response) => {
        alert(response.data)
        state.publish = []
        state.selectAll = false
        state.selected = []
        state.mutiPubDialog = false
      }).catch((error) => {
        alert(error.response.data.error)
      })
    } else {
      alert('请勾选')
    }
  }
}

const actions = {
  toggleAll ({commit}) {
    commit('toggleAll')
  },
  loadPublishList ({commit}) {
    commit('loadPublishList')
  },
  doMutiPublish ({commit}) {
    commit('doMutiPublish')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
